# -*- coding: UTF-8 -*- #
"""
-------真正的勇士， 敢于直面惨淡的Warning、 敢于正视淋漓的Error-----
@author : 贾鹏博
@file : 11、header、cookie 处理.py
@time : 2022/5/15 9:37
"""

# Cookie简介:
"""
Cookie使用场景：
    在接口测试过程中，很多情况，需要发送的请求附带cookies，才会得到正常的响应结果。
    所以用 python + requests 进行接口自动化测试也是同理，需要在构造接口测试用例时加入cookie
"""

# =====================================   传递 cookie 的两种方式  ===================================================
# 通过请求头信息传递  - header :    "Cookie":"hogwarts=school"
# 通过请求的关键字参数 cookies 传递  - cookie_data

import requests


# ========================================    自定义 header  ===========================================================
def test_demo():
    url = 'https://httpbin.testing-studio.com/cookies'
    # 定制请求头
    header = {"Cookie": "hogwarts=school",
              'User-Agent': 'hogwarts'}
    r = requests.get(url=url, headers=header)
    # {'User-Agent': 'hogwarts', 'Accept-Encoding': 'gzip, deflate, br', 'Accept': '*/*', 'Connection': 'keep-alive', 'Cookie': 'hogwarts=school'}
    print(r.request.headers)
    assert r.json()['cookies']['hogwarts'] == 'school'


# =====================================================  cookie  ========================================================
def test_cookie():
    url = 'https://httpbin.testing-studio.com/cookies'
    header = {'User-Agent': 'hogwarts'}
    cookie_data = {"hogwarts": "school",
                   "teacher": "AD"}
    r = requests.get(url=url, headers=header, cookies=cookie_data)
    # {'User-Agent': 'hogwarts', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Cookie': 'hogwarts=school; teacher=AD'}
    print(r.request.headers)
    assert r.json()['cookies']['teacher'] == 'AD'
